package za.co.trojanc.ini_lib;
/**
 * A class representing a field in an ini file.
 * @author Charl
 * @since 09 Sep 2009
 */
public class INIField {
	/** Name of field */
	private String name;
	/** Value of field */
	private String value;
	/** Text that preceded this field in the ini file */
	private String preText;
	
	/**
	 * Creates a new instance of <code>INIField</code>.
	 * @param name Name of this field.
	 */
	public INIField(String name) {
		this(name, null);
	}
	
	/**
	 * Creates a new instance of <code>INIField</code>.
	 * @param name Name of this field.
	 * @param value Value of this field.
	 */
	public INIField(String name, String value) {
		this.name = name;
		this.value = value;
	}
	
	/**
	 * Gets the name of this field.
	 * @return Name of field.
	 */
	public String getName() {
		return this.name;
	}
	
	/**
	 * Gets the value of this field.
	 * @return Value of this field.
	 */
	public String getValue() {
		return this.value;
	}
	
	/**
	 * Sets the value of this field.
	 * @param value Value of this field.
	 */
	public void setValue(String value) {
		this.value = value;
	}
	
	/**
	 * Sets the text that preceded this field in the ini file.
	 * @param text Text.
	 */
	public void setPreText(String text) {
		this.preText = text;
	}
	
	/**
	 * Gets the text that preceeded this field in the ini file.
	 * @return Text.
	 */
	public String getPreText() {
		return this.preText;
	}
	
	/**
	 * Gets the String value of this field.
	 * @param defaultValue Default value if this field has a null value.
	 * @return
	 */
	protected String getStringValue(String defaultValue) {
		return (this.getValue() == null ? defaultValue : this.getValue());
	}
	
	/**
	 * Gets the int value of this field.
	 * @param defaultValue value if this field is not set, or does not represent an int.
	 * @return
	 */
	protected int getIntValue(int defaultValue) {
		try {
			int num = Integer.parseInt(this.getValue());
			return num;
		}
		catch (NumberFormatException nfe){
			return defaultValue;
		}
	}
	
	/**
	 * Gets the boolean value of this field.
	 * @param defaultValue Default value if the value is not set, or a valid boolean value.
	 * @return
	 */
	protected boolean getBooleanValue(boolean defaultValue){
		try {
			return Boolean.parseBoolean(this.getValue());
		}
		catch (NumberFormatException nfe){
			return defaultValue;
		}
		
	}

	/**
	 * Gets the long value of this field.
	 * @param defaultValue value if this field is not set, or does not represent a long.
	 * @return
	 */
	protected long getLongValue(long defaultValue) {
		try {
			long num = Long.parseLong(this.getValue());
			return num;
		}
		catch (NumberFormatException nfe){
			return defaultValue;
		}
	}
	
	protected <E> E getValueAs(Class<E> clazz, E defaultValue){
		
		if(clazz == String.class){
			return (E)this.getStringValue((String)defaultValue);
		}
		else if (clazz == Boolean.class){
			return (E)(Boolean)this.getBooleanValue((Boolean)defaultValue);
		}
		else if (clazz == Long.class){
			return (E)(Long)this.getLongValue((Long)defaultValue);
		}
		else if (clazz == Integer.class){
			return (E)(Integer)this.getIntValue((Integer)defaultValue);
		}
		return null;
	}
	
}
